package com.example.show_auto.ui.utils

import android.os.Build
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

/**
 * 设备兼容性工具类
 * 用于处理不同设备和厂商的兼容性问题
 */
object DeviceCompatibility {
    
    /**
     * 检查是否为OPPO设备
     */
    fun isOppoDevice(): Boolean {
        return Build.MANUFACTURER.equals("OPPO", ignoreCase = true) ||
               Build.BRAND.equals("OPPO", ignoreCase = true)
    }
    
    /**
     * 检查是否为ColorOS系统
     */
    fun isColorOS(): Boolean {
        return try {
            val colorOSVersion = Build.VERSION.INCREMENTAL
            colorOSVersion.contains("ColorOS", ignoreCase = true) ||
            System.getProperty("ro.build.version.opporom") != null
        } catch (e: Exception) {
            false
        }
    }
    
    /**
     * 检查是否为问题机型
     */
    fun isProblematicDevice(): Boolean {
        return isOppoDevice() || isColorOS() || 
               Build.MODEL.contains("Reno", ignoreCase = true)
    }
    
    /**
     * 获取适配后的间距
     */
    @Composable
    fun getAdaptiveSpacing(normalSpacing: Dp): Dp {
        return if (isProblematicDevice()) {
            normalSpacing * 0.8f // 减少20%的间距
        } else {
            normalSpacing
        }
    }
    
    /**
     * 获取适配后的高度
     */
    @Composable
    fun getAdaptiveHeight(normalHeight: Dp): Dp {
        return if (isProblematicDevice()) {
            normalHeight * 0.9f // 减少10%的高度
        } else {
            normalHeight
        }
    }
    
    /**
     * 获取适配后的卡片颜色
     */
    @Composable
    fun getAdaptiveCardColors(): CardColors {
        return if (isProblematicDevice()) {
            // 对于问题设备使用更明确的颜色对比
            CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surface,
                contentColor = MaterialTheme.colorScheme.onSurface
            )
        } else {
            CardDefaults.cardColors()
        }
    }
    
    /**
     * 获取适配后的文本字段颜色
     */
    @Composable
    fun getAdaptiveTextFieldColors(): TextFieldColors {
        return if (isProblematicDevice()) {
            OutlinedTextFieldDefaults.colors(
                focusedBorderColor = MaterialTheme.colorScheme.primary,
                unfocusedBorderColor = MaterialTheme.colorScheme.outline,
                focusedLabelColor = MaterialTheme.colorScheme.primary,
                unfocusedLabelColor = MaterialTheme.colorScheme.onSurfaceVariant,
                cursorColor = MaterialTheme.colorScheme.primary
            )
        } else {
            OutlinedTextFieldDefaults.colors()
        }
    }
    
    /**
     * 检查是否需要强制重绘
     */
    @Composable
    fun shouldForceRecomposition(): Boolean {
        return isProblematicDevice()
    }
    
    /**
     * 获取屏幕密度信息
     */
    @Composable
    fun getScreenDensityInfo(): String {
        val density = LocalDensity.current.density
        val configuration = LocalConfiguration.current
        return "Density: $density, Screen: ${configuration.screenWidthDp}x${configuration.screenHeightDp}dp"
    }
    
    /**
     * 获取设备信息
     */
    fun getDeviceInfo(): String {
        return "Brand: ${Build.BRAND}, Model: ${Build.MODEL}, Manufacturer: ${Build.MANUFACTURER}, " +
               "Android: ${Build.VERSION.RELEASE}, SDK: ${Build.VERSION.SDK_INT}"
    }
    
    /**
     * 适配后的Modifier扩展
     */
    @Composable
    fun Modifier.adaptiveLayout(): Modifier {
        return if (isProblematicDevice()) {
            this.fillMaxWidth()
                .wrapContentHeight()
        } else {
            this
        }
    }
    
    /**
     * 强制重绘的Composable包装器
     */
    @Composable
    fun AdaptiveContainer(
        modifier: Modifier = Modifier,
        content: @Composable () -> Unit
    ) {
        if (shouldForceRecomposition()) {
            // 对于问题设备，添加额外的重绘触发
            var recompositionTrigger by remember { mutableStateOf(0) }
            
            LaunchedEffect(Unit) {
                // 延迟触发重绘
                kotlinx.coroutines.delay(100)
                recompositionTrigger++
            }
            
            Box(modifier = modifier) {
                content()
            }
        } else {
            Box(modifier = modifier) {
                content()
            }
        }
    }
    
    /**
     * 调试信息显示
     */
    @Composable
    fun DebugInfo() {
        // 调试信息已移除，不显示任何内容
    }
}
